<template>
  <div class="login">
    <el-card class="box-card">
      <!-- 标题 -->
      <div slot="header" class="clearfix">
        <span>黑马面经运营后台</span>
      </div>
      <!-- 放内容 -->
      <el-form
        status-icon
        :model="userinfo"
        label-position="top"
        :rules="rules"
        ref="ruleForm"
        label-width="60px"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            type="text"
            v-model="userinfo.username"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            v-model="userinfo.password"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item style="text-align: center" size="large">
          <el-button type="primary" @click="onSubmit">登录</el-button>
          <el-button>重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { LoginApi } from '@/api/login'
export default {
  name: 'LogIn',
  data () {
    // 自定义效验规则
    const validatePass = (rule, value, callback) => {
      if (!/^[\w-]{5,16}$/.test(value)) {
        callback(new Error('用户名必须是5到16位（字母，数字，下划线，减号）'))
      } else {
        callback()
      }
    }
    return {
      userinfo: {
        username: 'admin',
        password: 'admin'
      },
      // 规则
      rules: {
        username: [
          { validator: validatePass, trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 16, message: '长度在 3 到 16 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    onSubmit () {
      // console.log(1)

      // 兜底效验
      this.$refs.ruleForm.validate(async (valid) => {
        // console.log(valid )
        if (valid) {
          // alert('submit!')
          // 发送 axios 请求

          try {
            const res = await LoginApi(this.userinfo)
            console.log(res)

            // 保存token
            this.$store.commit('user/updateToken', res.data.data.token)
            this.$notify({
              title: '成功',
              message: '登录成功',
              type: 'success'
            })
            this.$router.push('/')
          } catch (error) {
            // console.log(error)

            this.$notify.error({
              title: '错误',
              message: error.response.data.message
            })
          }
        }
      })
    }
  }

}
</script>

<style lang='scss' scoped>
.login {
  min-height: 100vh;
  background: url(@/assets/login-bg.svg) no-repeat center;
  background-size: cover;
  display: flex;
  align-items: center;
  justify-content: center;
  .el-card {
    width: 420px;
    ::v-deep .el-card__header {
      height: 80px;
      background: rgba(114, 124, 245, 1);
      text-align: center;
      line-height: 40px;
      color: #fff;
      font-size: 18px;
    }
  }
  ::v-deep .is-always-shadow {
    border: 0px solid rgba(114, 124, 245, 1);
  }
  .el-form {
    padding: 0 20px;
  }
}
</style>
